<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">
      <template #beforeTable>
        <button-group :button-groups="buttonGroups" :crud-ref="crudRef" :selected="selected" :currentStatus="currentStatus" />
      </template>
      <template #operationCell="{ record }">
        <a-link v-if="[3,4].includes(record.status) " @click="singleDownload(record)"><icon-download />下载</a-link>
      </template>
    </ma-crud>
    <add-modal ref="addModalRef"></add-modal>
  </div>
</template>
<script setup>
import { ref, reactive,provide,computed } from 'vue'
import sendOrder from '@/api/document/sendOrder'
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import * as common from '@/utils/common'
import ButtonGroup from "@cps/actions/buttonGroup.vue";
import commonApi from '@/api/common'
import uploadConfig from '@/config/upload'
const crudRef = ref()
const selected = computed(() => crudRef.value?.selecteds)
const currentStatus = ref(1)
const buttonGroups = [
  {
    title: '下单',
    api: sendOrder.saveOrder,
    auth: ['document:sendOrder:saveOrder'],
    hideStatus:[2,3,4,6,7]
  },
  {
    title: '作废',
    api: sendOrder.cancel,
    auth: ['document:sendOrder:cancel'],
    hideStatus:[7]
  },
  {
    title: '单号回填',
    api: sendOrder.saveOrder,
    auth: ['document:sendOrder:saveOrder'],
    hideStatus:[1,3,4,5,7]
  },
]


const options = reactive({
  id: 'send_order',
  rowSelection: {
    showCheckedAll: true
  },
  operationColumn: true,
  pk: 'id',
  tabs: {
    dataIndex: "status",
    onChange: (value) => {
      currentStatus.value = value
    },
  },
  formOption: {
    viewType: 'drawer',
    width: 600
  },
  api: sendOrder.getList,
})

const columns = reactive([
  {
    title: "主键",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true,
  },
  {
    title: "状态",
    dataIndex: "status",
    formType: "select",
    search: true,
    hide: true,
    addDefaultValue: 1,
    dict: {
      name: "send_order_status",
      props: {
        label: "title",
        value: "key"
      },
      translation: true
    }
  },
  {
    title: "运单号",
    dataIndex: "shipment.shipment_no",
    formType: "input",
    search: true,
    addDisplay: false,
    editDisplay: false,
  },
  {
    title: "费用",
    dataIndex: "price",
    formType: "input",
  },
  {
    title: "外部订单号",
    dataIndex: "order_id",
    formType: "input",
    search: true
  },
  {
    title: "服务",
    dataIndex: "service_type",
    formType: "input",
    search: true
  },
  {
    title: "主单号",
    dataIndex: "tracking_no",
    formType: "input",
    search: true
  },
  {
    title: "货箱数量",
    dataIndex: "parcel_count",
    formType: "input",
  },
  {
    title: "创建者",
    dataIndex: "created_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
  },
  {
    title: "更新者",
    dataIndex: "updated_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "创建时间",
    dataIndex: "created_at",
    search: true, formType: "range",
    addDisplay: false,
    editDisplay: false,
    showTime: true,
  },
  {
    title: "更新时间",
    dataIndex: "updated_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "删除时间",
    dataIndex: "deleted_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "备注",
    dataIndex: "remark",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
  },
  {
    title: "错误",
    dataIndex: "error",
    formType: "textarea",
    ellipsis:false,
    addDisplay: false,
    editDisplay: false,
  },
])
const getStoreMode = (mode) => {
    return uploadConfig.storageMode[mode.toString()]
  }
  // const downloadZip = async () =>{
  //   let response = await sendOrder.downloadZip({ids:[record.value.id]})
  //   let contentType = response.headers['content-type'].toLowerCase().split(';')[0];
  //   let disposition = response.headers['content-disposition'].split(';')[2].split('=')[1].split('\'')[2]
  //   saveAs(new Blob([response.data], {type: contentType}), decodeURIComponent(disposition))
  // }
const singleDownload = async (record) => {
    if(record.upload.id == undefined){
      Message.error('文件还未同步')
      return;
    }
    let url = ''
    let storeMode = getStoreMode(record.upload.storage_mode)
    console.log(storeMode)
    if (storeMode !== 'LOCAL') {
      Message.info('处理中...')
      const domain = uploadConfig.storage[storeMode]
      url = domain + record.upload.url
    } else {
      //文件上传在本地则请求后端下载
      Message.info('请求服务器下载文件中...')
      url = 'system/downloadById?id=' + record.id+'&type=attachment'
    }
    const response = await commonApi.download(url, 'get')
    if (response) {
      tool.download(response, (storeMode !== 'LOCAL') ? record.upload.origin_name : '')
      Message.success('请求成功，文件开始下载')
      sendOrder.updateStatus(record.id,{status:4})
      crudRef.value.refresh()
    } else {
      Message.error('文件下载失败')
    }
  }
</script>
<script> export default { name: 'document:sendOrder' } </script>